package ypmits.handcursor {
	import flash.display.Bitmap;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.ui.Mouse;

	/**
	 * @author Dave Lenz
	 * 
	 * Some of the handcursors are courtesy of http://blog.thecombustionchamber.com/2009/08/hand_cursor_alternates.html
	 */
	public class HandCursor extends Sprite implements IHandCursor {
		[Embed(source="closedhand.gif")]
		private var closedhandcursor : Class;

		[Embed(source="openhand.gif")]
		private var openhandcursor : Class;
		
		private var bm : Bitmap;
		
		public function custom( bitmap : Bitmap ) : void {
			setCursor( bitmap );
		}
		
		
		public function showOpenHand() : void {
			setCursor( new openhandcursor as Bitmap );
		}
		
		
		public function showClosedHand() : void {
			setCursor( new closedhandcursor as Bitmap );
		}

		
		public function dispose() : void {
			Mouse.show();
			removeChild( bm );
			bm = null;
			mouseChildren = mouseEnabled = true;
			removeEventListener( Event.ENTER_FRAME, enter );
		}
		
		
		private function setCursor( bitmap : Bitmap ) : void {
			bm = bitmap;
			bm.pixelSnapping = PixelSnapping.ALWAYS;
			bm.smoothing = false;
			bm.x = -(bm.width * .5);
			bm.y = -(bm.height * .5);
			addChild( bm );
			
			Mouse.hide();
			mouseChildren = mouseEnabled = false;
			addEventListener( Event.ENTER_FRAME, enter );
		}
		
		private function enter( e : Event ) : void {
			x = parent.mouseX;
			y = parent.mouseY;
		}
	}
}
